<template>
  <div class="app-container">
    <el-form class="formList"  :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="104px">
      <el-row>

        <!-- <el-col :span="8"> -->
          <el-form-item label="定义展示名称" prop="definitionShowname">
            <el-input v-model="queryParams.definitionShowname" placeholder="请输入定义展示名称" clearable size="small"
              @keyup.enter.native="handleQuery" />
          </el-form-item>
        <!-- </el-col> -->

       <!-- <el-col :span="8"> -->
         <el-form-item label="状态" prop="status">
           <el-select v-model="queryParams.status" placeholder="请选择状态" clearable size="small">
             <el-option
               v-for="dict in statusOptions"
               :key="dict.dictValue"
               :label="dict.dictLabel"
               :value="dict.dictValue"
             />
           </el-select>
         </el-form-item>

         <el-form-item style="margin-left: 30px;">
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </el-form-item>
       <!-- </el-col> -->

      </el-row>
      <!-- <el-row>

      </el-row> -->
    </el-form>

    <el-row class="row-mbinfo">
      <!-- <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['groupDefinition:groupDefinition:add']">新增</el-button>
      </el-col> -->
     <!-- <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['groupDefinition:groupDefinition:remove']">删除</el-button>
      </el-col> -->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>
<div class="divTable">
    <el-table border v-loading="loading" :data="groupDefinitionList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="特征名称" align="center" prop="definitionName" :show-overflow-tooltip="true"  />
      <el-table-column label="特征展示名称" align="center" prop="definitionShowname" :show-overflow-tooltip="true"  />
      <el-table-column label="特征描述" align="center" prop="definitionDesc" :show-overflow-tooltip="true"  />
      <el-table-column label="特征类型" align="center" prop="definitionType" :formatter="definitionTypeFormat"/>
      <!-- <el-table-column label="特征类型" align="center" prop="definitionType" :show-overflow-tooltip="true"  /> -->
      <el-table-column label="修改时间" align="center" prop="updateTime" width="180" >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.updateTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" prop="dataStatus">
        <template slot-scope="scope">
          <el-switch v-model="scope.row.dataStatus" active-value="1" inactive-value="0"
                     @change="handleStatusChange(scope.row)"></el-switch>
        </template>
      </el-table-column>
<!--      <el-table-column label="状态" align="center" prop="dataStatus" :show-overflow-tooltip="true"
        sortable /> -->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <!-- <el-button size="mini" type="text" icon="el-icon-zoom-in" @click="handleShowDetail(scope.row)"
            v-hasPermi="['groupDefinition:groupDefinition:query']">查看</el-button> -->
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['groupDefinition:groupDefinition:edit']">修改</el-button>
<!--          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"-->
<!--            v-hasPermi="['groupDefinition:groupDefinition:remove']">删除</el-button>-->
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />
</div>
    <!-- 添加或修改客户分群定义信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body @keyup.enter.native="submitForm">
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-row>
          <el-col :span="24" >
            <el-form-item label="特征名称" prop="definitionName" v-if="this.openType == 'add' ">
              <el-input v-model="form.definitionName" placeholder="请输入特征名称"/>
            </el-form-item>
            <el-form-item label="特征名称" prop="definitionName" v-if="this.openType == 'edit' ">
              <el-input v-model="form.definitionName" disabled placeholder="请输入特征名称" />
            </el-form-item>
          </el-col>
        </el-row>
       <el-row>
         <el-col :span="24" >
           <el-form-item label="特征展示名称" prop="definitionShowname">
              <el-input v-model="form.definitionShowname" placeholder="请输入特征展示名称" />
            </el-form-item>
         </el-col>
       </el-row>
      <el-row>
          <el-col :span="24" >
            <el-form-item label="特征描述" prop="definitionDesc">
              <el-input type="textarea" v-model="form.definitionDesc" placeholder="请输入特征简介"/>
            </el-form-item>
          </el-col>
      </el-row>
      <el-row>
        <el-col :span="16" >
          <el-form-item label="特征单位" prop="definitionUnit">
            <el-input v-model="form.definitionUnit" placeholder="请输入特征单位"/>
          </el-form-item>
        </el-col>
          <el-col :span="8">
            <el-tooltip content="如多少天访问,单位为 “天” " effect="light" placement="top">
              <i
                class="el-icon-question"
                style="margin-top: 10px; margin-left: 10px"
              >
              </i>
            </el-tooltip>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24" >
            <el-form-item label="特征类型" prop="definitionType">
              <el-select v-model="form.definitionType" placeholder="请选择特征类型">
                <el-option
                  v-for="dict in definitionTypeOptions"
                  :key="dict.dictValue"
                  :label="dict.dictLabel"
                  :value="dict.dictValue"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 查看客户分群定义信息对话框 -->
    <el-dialog :title="viewTitle" :visible.sync="viewOpen" width="700px" append-to-body>
      <el-form ref="detailForm" label-width="100px">
        <el-row>
          <el-col :span="12" v-for="(item, index) in showData" :key="index" style="margin-top:-15px">
            <el-form-item :label="`${item.label}：`">
              <span>{{item.value}}</span>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" size="mini" @click="handCloseDetail">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
  import {
    listGroupDefinition,
    getGroupDefinition,
    delGroupDefinition,
    addGroupDefinition,
    updateGroupDefinition,
    updateGroupDefinitionStatus
  } from "@/api/customer/groupDefinition";

  export default {
    name: "GroupDefinition",
    components: {},
    data() {
      return {
        // 遮罩层
        loading: true,
        // 选中数组
        //添加或修改状态
        openType:"",
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 客户分群定义信息表格数据
        groupDefinitionList: [],
        // 弹出层标题
        title: "",
        //查看详细标题
        viewTitle: "",
        // 是否显示弹出层
        open: false,
        //是否显示查看详细弹出层
        viewOpen: false,
        //查看详细数据
        showData: [],
        // 状态 1有效 0无效字典
        statusOptions: [],
        //特征类型
        definitionTypeOptions:[],
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          definitionName: null,
          definitionShowname: null,
          definitionValue: null,
          createTime: null,
          updateTime: null,
          createUser: null,
          updateUser: null,
          dataStatus: null,
          createName: null,
          updateName: null
        },
        columns: [
          {
            label: '定义名称',
            dataIndex: 'definitionName',
          },
          {
            label: '定义展示名称',
            dataIndex: 'definitionShowname',
          },
        ],
        // 表单参数
        form: {},
        // 表单校验
        rules: {}
      };
    },
    created() {
      this.getDicts("sys_data_status").then(response => {
        this.statusOptions = response.data;
      });
      this.getDicts("customer_groupDefinition_Type").then(response => {
        this.definitionTypeOptions = response.data;
      });
      this.getList();
    },
    methods: {
      // 状态 1有效 0无效字典翻译
      statusFormat(row, column) {
        return this.selectDictLabel(this.statusOptions, row.status);
      },
      // 特征类型字典翻译
      definitionTypeFormat(row, column) {
        return this.selectDictLabel(this.definitionTypeOptions, row.definitionType);
      },
      /** 查询客户分群定义信息列表 */
      getList() {
        this.loading = true;
        listGroupDefinition(this.queryParams).then(response => {
          this.groupDefinitionList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          definitionUid: null,
          definitionName: null,
          definitionShowname: null,
          definitionValue: null,
          createTime: null,
          updateTime: null,
          createUser: null,
          updateUser: null,
          dataStatus: "0",
          createName: null,
          updateName: null
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.definitionUid)
        this.names = selection.map(item => item.definitionShowname)
        this.single = selection.length !== 1
        this.multiple = !selection.length
        //用于禁用删除
        if (this.multiple == false) {
          for (var i = 0; i < selection.length; i++) {
            var element = selection[i];
            if ("1" === element.dataStatus) {
              this.multiple = true;
              break;
            }
          }
        }
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.openType = 'add';
        this.reset();
        this.open = true;
        this.title = "添加客户分群定义信息";
      },
      /** 查看详情按钮操作 **/
      handleShowDetail(row) {
        this.viewOpen = true;
        this.viewTitle = "查看客户分群定义信息";
        this.showData = [];
        this.columns.forEach(item => {
          this.showData.push({
            label: item.label,
            value: row[item.dataIndex]
          })
        })
      },
      handCloseDetail() {
        this.viewOpen = false
        this.showData = []
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.openType = 'edit';
        this.reset();
        const definitionUid = row.definitionUid || this.ids
        getGroupDefinition(definitionUid).then(response => {
          this.form = response.data;
          this.open = true;
          this.title = "修改客户分群定义信息";
        });
      },
      /** 提交按钮 */
      submitForm() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            if (this.form.definitionUid != null) {
              updateGroupDefinition(this.form).then(response => {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              });
            } else {
              addGroupDefinition(this.form).then(response => {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              });
            }
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const definitionUids = row.definitionUid || this.ids;
        this.$confirm('是否确认删除客户分群特征名称为"' + row.definitionShowname + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delGroupDefinition(definitionUids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
      },
      /** 导出按钮操作 */
      handleExport() {
        this.download('groupDefinition/groupDefinition/export', {
          ...this.queryParams
        }, `groupDefinition_groupDefinition.xlsx`)
      },
      // 状态修改
      handleStatusChange(row) {
        let text = row.dataStatus === "0" ? "无效" : "有效";
        this.$confirm('确定要把"' + row.definitionShowname + '"设为' + text + '吗？', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return updateGroupDefinitionStatus(row.definitionUid, row.dataStatus);
        }).then(() => {
          this.msgSuccess("操作成功");
          this.getList();
        }).catch(function() {
          row.dataStatus = row.dataStatus === "0" ? "1" : "0";
        });
      },
    }
  };
</script>
